package com.woniu.cloud.marketingassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.entity.TCoupon;
import com.woniu.cloud.marketingassembly.mapper.TCouponMapper;
import com.woniu.cloud.marketingassembly.service.ITCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-09-22
 */
@Service
@Slf4j
public class TCouponServiceImpl extends ServiceImpl<TCouponMapper, TCoupon> implements ITCouponService {


    @Resource
    private TCouponMapper couponMapper;
    @Override
    public Result getCouponByConponId(Integer conponId) {

        TCoupon coupon = couponMapper.selectById(conponId);
        if (null!=coupon) {
            return Result.success("优惠券存在",coupon);
        }
        return Result.fail("优惠券不存在",null);

    }

    @Override
    public Result insertCoupon(TCoupon coupon) {
        QueryWrapper<TCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_name",coupon.getCouponName());
        wrapper.eq("coupon_Price",coupon.getCouponPrice());
        List<TCoupon> list = couponMapper.selectList(wrapper);
        if (0!=list.size()) {
            return Result.fail("优惠券存在",false);
        }
        int insert = couponMapper.insert(coupon);
        if (0!=insert) {
            log.info("添加成功->{}",coupon);
            return Result.success("添加成功",true);
        }
        return Result.fail("添加失败",false);

    }

    @Override
    public Result deleteCouponByConponId(Integer conponId) {
        log.info("正在删除->conponId:{}",conponId);
        int isDelete = couponMapper.deleteById(conponId);
        if (1==isDelete) {
            log.info("删除成功->");
            return Result.success("删除成功",null);
        }
        log.info("删除失败->");
        return Result.fail("删除失败",null);

    }

    @Override
    public TCoupon getCouponByPrice(Double price) {
        QueryWrapper<TCoupon> tCouponQueryWrapper = new QueryWrapper<>();
        tCouponQueryWrapper.eq("coupon_price",price);
        List<TCoupon> tCoupons = getBaseMapper().selectList(tCouponQueryWrapper);
        return tCoupons.get(0);
    }
}
